<?php
/*
Zoo Management Simulation
This file determines the previous page the user was one and manages data accordingly.
If the user was from the Cage UI in which the user wanted to only add cage entries it would only display the add cage entry form
If this is not the case the data from the previous page is inserted into the database creating a new cage
The form sends the information placed in the fields provided to addCageEntryProcess.php
*/

include("dbconnect.php");
if ($_REQUEST['submit'] == "Return to Administator page"){
	header('Location: setupAdmin.php');
}


if ($_REQUEST['submit'] == "Add Entries"){
$name = $_REQUEST['name'];
$size = $_REQUEST['size'];
$type = $_REQUEST['type'];
$numberofEntries = $_REQUEST['numentry'];
$exhibitName = $_REQUEST['exhibitname'];
$exhibitDesc = $_REQUEST['exhibitdesc'];
$humanDetector = $_REQUEST['humandetector'];
$animalDetector = $_REQUEST['animaldetector'];

$idSql = "SELECT max(enclosureId)+1 FROM enclosure";
foreach ($dbh->query($idSql) as $row)
	{
		$id = $row[0];
	}

$sql = "INSERT INTO `enclosure`(`enclosureId`, `enclosureName`, `enclosureSize`, `enclosureTypeId`, `exhibitName`, `exhibitDesc`,  `numEnclosureEntries`, `enclosureHasAnimal`, `enclosureHasPerson`, `isEnclosureLocked`, `isEnclosureLightsOn`) VALUES (".$id.",'".$name."',".$size.",".$type.",'".$exhibitName."','".$exhibitDesc."', ".$numberofEntries.", ".$animalDetector.", ".$humanDetector.", 0, 0)";
$dbh->exec($sql);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add Cage Entries</title>
<link href="mainstyle.css" rel="stylesheet" type="text/css" />
</head>

<body>
<h1>Add Cage Entries</h1>

<form id="addCageEntryForm" name="addCageEntryForm" method="post" enctype="multipart/form-data" action="addCageEntryProcess.php">
<table id="zootable" width="100%" border="1">
	  <tr>
	    <td id="tableheader">Entry</td>
	    <td id="tableheader"><label for="doortype">Entry Type</label></td>
	    <td id="tableheader"><label for="doorstatus">Entry Status</label></td>
      </tr>
      
      <?php
      $entrynumber = 1;
	  while($entrynumber <= $numberofEntries){
      	echo "<tr>";
	    echo "<td>". $entrynumber . "</td>";
		
	    echo "<td>
		<select name='doortype".$entrynumber."' id='doortype".$entrynumber."'> <option value='1'>Single Door</option>
		<option value='2'>Double Door</option>
		<option value='3'>Hatch Door</option>
		</select>
		</td>";
		
	    echo "<td>
		<select name='doorstatus".$entrynumber."' id='doorstatus".$entrynumber."'> <option value='1'>Open</option>
		<option value='0' selected='selected'>Closed</option>
		</select>
		</td>";
      echo "</tr>";
	  $entrynumber+=1;
	  }
	  echo '<input type="hidden" id="numentry" name="numentry" value="'.$numberofEntries.'" />';
	  	  echo '<input type="hidden" id="enclosureid" name="enclosureid" value="'.$id.'" />';
	  $dbh = null;?>
</table>
<input type="submit" name="submit" id="submit" value="Add Cage Entries" />
<input type="submit" name="submit" id="submit" value="Return to Administator page" />
</form>

</body>
</html>